package cn.edu.nit.sys.service.impl;

import cn.edu.nit.sys.entity.Message;
import cn.edu.nit.sys.mapper.MessageMapper;
import cn.edu.nit.sys.service.MessageService;
import cn.edu.nit.sys.utils.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaobai
 * @Date 2022/11/2 20:16
 * @Version 1.0.0
 */
@Slf4j
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Override
    @Async
    public void saveMessage(Message message) {
        baseMapper.insert(message);
        log.info("消息插入数据库");
    }

    @Override
    public void updateMessageStatus(Message message) {
        log.info("修改消息状态");
    }

    @Override
    public R getSysMessageList(Page<Map<String, String>> page, String uid) {
        Page<Map<String, String>> sysMessageList = baseMapper.getSysMessageList(page, uid);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", sysMessageList.getTotal());
        jsonObject.put("list", sysMessageList.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    public R changeReadStatus(String id) {
        baseMapper.changeReadStatus(id);
        return R.ok("");
    }

    @Override
    public List<Map<String, String>> getNoReadMessage(String userId) {
        return baseMapper.getNoReadMessage(userId);
    }

    @Override
    public R getNotReadMessage(String id) {
        List<Map<String, Object>> notReadNotice = baseMapper.notReadNotice(id);
        List<Map<String, Object>> messageUsers = baseMapper.getMessageUsers(id);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("notice", notReadNotice);
        List<Map<String, Object>> data = new ArrayList<>();
        for(Map<String, Object> user : messageUsers) {
            String source = user.get("source").toString();
            Map<String, Object> userMessage = baseMapper.getUserMessage(id, source);
            user.put("badge", userMessage.get("badge"));
            user.put("label", userMessage.get("label"));
            data.add(user);
        }
        jsonObject.put("msg", data);
        return R.ok(jsonObject);
    }

    @Override
    public R getMessageList(Page<Map<String, String>> page, String userId1, String userId2) {
        Page<Map<String, String>> list = baseMapper.getMessageList(page, userId1, userId2);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", list.getTotal());
        jsonObject.put("list", list.getRecords());
        return R.ok(jsonObject);
    }

    @Override
    public R readMessage(String target, String source) {
        baseMapper.readMessage(target, source);
        return null;
    }
}
